package view;

import java.awt.Color;






import javax.swing.BorderFactory;


import javax.swing.JButton;

import javax.swing.JLabel;


import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;


import javax.swing.table.DefaultTableModel;

import net.miginfocom.swing.MigLayout;

/**
 * Panel to list the emails in a particular folder
 * 
 * @author MUSAB
 * @since May, 2013 
 * @version $Revision: 1.0 $
 */
public class ListingView extends JPanel {
	
	
	// Folder label
	private JLabel folderName;
	
	// Toolbar buttons
	private JButton deleteBtn, moveBtn, searchBtn;
	
	// The emails list
	private DefaultTableModel emails;
	
	// The emails  table
	private JTable emailsTable;
	
	/**
	 * Constructor
	
	 * @param migLayout migLayout the layout for the panel
	 */
	public ListingView(MigLayout migLayout){
		
		// Calling super constructor
		super(migLayout);
		
		/**
		 * Creating the toolbar
		 */
		deleteBtn = new JButton("Delete", IconsManager.getImageIcon("small/cross.png"));
		deleteBtn.setToolTipText("Click this button to delete the selected emails");
		moveBtn = new JButton("Move", IconsManager.getImageIcon("small/folder-arrow.png"));
		folderName = new JLabel("");
		JPanel toolbar = new JPanel(new MigLayout("fillx", ""));
		toolbar.setBorder(BorderFactory.createMatteBorder(1, 1, 1, 1, Color.LIGHT_GRAY));
		toolbar.add(folderName);
		
		searchBtn = new JButton("Search...",IconsManager.getImageIcon("small/magnifier.png"));
		toolbar.add(searchBtn, "al right, push");
		
		toolbar.add(moveBtn, "al right");
		toolbar.add(deleteBtn, "al right");
		this.add(toolbar, "wrap, growx");
		
		/**
		 * Creating the email list table
		 */
		String[] columnNames = {
				"No",
                "To",
                "From",
                "Date/time",
                "Subject"
		};

		emails = new DefaultTableModel();
		emails.setColumnIdentifiers(columnNames);
		
		// Uneditable and non-reorderable table
		emailsTable = new JTable(emails){
			@Override
			public boolean isCellEditable(int row, int column){  
			    return false;  
			}
		};
		emailsTable.getTableHeader().setReorderingAllowed(false);

		
		JScrollPane emailsScrollPane = new JScrollPane(emailsTable);
		this.add(emailsScrollPane, "growx, height 100::200"); // max height of listing pane
	}

	/**
	 * Returns the move button
	 * 
	
	
	 * @return JButton returns the move button */
	public JButton getMoveBtn() {
		return moveBtn;
	}

	/**
	 * Returns the delete button
	 * 
	
	 * @return JButton returns the delete button */
	public JButton getDeleteBtn() {
		return deleteBtn;
	}	
	
	/**
	 * Returns the Emails JTable
	 * 
	
	 * @return JTable returns the table of email messages */
	public JTable getEmailsTable(){
		return emailsTable;
	}
	
	/**
	 * Returns the emails table model
	 * 
	
	 * @return DefaultTableModel returns the table model used */
	public DefaultTableModel getEmails(){
		return emails;
	}
	
	/**
	 * Clears the folder name and empties the list
	 * 
	
	
	 */
	public void clear(){
		folderName.setText("");
		// (from http://stackoverflow.com/questions/10413977/javaremoving-all-the-rows-of-defaulttablemodel)
		if (emails.getRowCount() > 0) {
		    for (int i = emails.getRowCount() - 1; i > -1; i--) {
		    	emails.removeRow(i);
		    }
		}
	}
	
	/**
	 * Sets the current folder name
	 * 
	
	
	 * @param name String
	 */
	public void setFolderName(String name) {
		folderName.setText(name.trim());
	}

	/**
	
	 * @return the searchBtn */
	public JButton getSearchBtn() {
		return searchBtn;
	}

}
